1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect;
18
19 import com.google.caliper.BeforeExperiment;
20 import com.google.caliper.Benchmark;
21 import com.google.caliper.Param;
22 import com.google.common.collect.BenchmarkHelpers.SetImpl;
23 import com.google.common.collect.CollectionBenchmarkSampleData.Element;
24
25 import java.util.Set;
26
27
28
29
30
31
32 public class SetIterationBenchmark {
33 @Param({ "3", "6", "11", "23", "45", "91", "181", "362", "724", "1448",
34 "2896", "5793", "11585", "23170", "46341", "92682", "185364", "370728",
35 "741455", "1482910", "2965821", "5931642"})
36 private int size;
37
38
39 @Param("1234")
40 private SpecialRandom random;
41
42 @Param({"Immutable", "Hash"})
43 private SetImpl impl;
44
45
46 private Set<Element> setToTest;
47
48 @BeforeExperiment void setUp() {
49 CollectionBenchmarkSampleData sampleData =
50 new CollectionBenchmarkSampleData(true, random, 0.8, size);
51 setToTest = impl.create(sampleData.getValuesInSet());
52 }
53
54 @Benchmark int iteration(int reps) {
55 int x = 0;
56
57 for (int i = 0; i < reps; i++) {
58 for (Element y : setToTest) {
59 x ^= System.identityHashCode(y);
60 }
61 }
62 return x;
63 }
64 }